import React, { PureComponent } from 'react';
import { connect } from 'dva';
import AssetReport from '../../../reportingServices/nonstandardReport/assetReport';

@connect(({ loading, ProductAnalysisM }) => ({ loading, ProductAnalysisM }))
export default class ReportC extends PureComponent {
  state = {
    tabList: [], //资负报告数据源
  };
  componentDidMount() {
    this.queryPortfolioList(this.props.productCode);
  }

  UNSAFE_componentWillReceiveProps(nextProps) {
    if (nextProps.productCode != this.props.productCode) {
      this.queryPortfolioList(nextProps.productCode);
    }
  }
  queryPortfolioList = (productCode) => {
    if (!productCode) return;
    this.props.dispatch({
      type: 'ProductAnalysisM/getProductGroup',
      payload: { productCode },
      callback: (data) => {
        if (data?.length) {
          let groupList = [];
          let formatData = (data = {}) => {
            groupList.push(data.portfolioCode);
            if (data.children && data.children.length) {
              data.children?.map((item) => {
                if (item.children && item.children.length) {
                  formatData(item);
                } else {
                  groupList.push(item.portfolioCode);
                }
              });
            }
          };
          formatData(data[0] || {});
          this.setState({
            tabList: [
              {
                key: data[0]?.key,
                portfolioCode: data[0]?.portfolioCode,
                portfolioName: data[0]?.portfolioName,
                productId: data[0]?.portfolioCode,
                groupList,
              },
            ],
          });
        } else {
          this.setState({
            tabList: [],
          });
        }
      },
    });
  };

  render() {
    const { tabList } = this.state;
    return (
      <div>
        <AssetReport type='nonstandard' tabList={tabList} getReportData={this.props.getReportData} />
      </div>
    );
  }
}
